package com.leeoohoo.user;

import com.oohoo.spacestationspringbootstarter.dto.query.EQ;
import com.oohoo.spacestationspringbootstarter.validate.GroupValidate;
import com.oohoo.spacestationspringbootstarter.validate.annotation.Verify;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Verify
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserVerify implements GroupValidate {

    private final MyUserService myUserService;


    @Override
    public boolean validate(Map<String, Object> map) {
        List<MyUser> list = myUserService.getButler().findList(EQ.find(MyUser.class)
                        .select(MyUser.class)
                        .where().eq(MyUser::getUsername, map.get("username"))
                        .or().eq(MyUser::getPhone, map.get("phone"))
                        .or().eq(MyUser::getEmail, map.get("email"))
                , MyUser.class);
        if (Objects.isNull(map.get("id"))) {
            return list.size() == 0;
        } else {
            return list.size() <= 1;
        }
    }
}